/*
 * Copyright (C) 2015 Karumi.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.karumi.dexter.listener.multi;

import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.listener.PopupDialogUtils;
import com.karumi.dexter.listener.SettingsClickListener;
import com.karumi.dexter.uitool.DexterLogUtil;
import com.karumi.dexter.uitool.ResTUtil;

import ohos.agp.components.Component;
import ohos.app.Context;

/**
 * Utility listener that shows a {@link PopupDialogUtils} with a custom text whenever a permission has been
 * denied
 */
public class PopupOnAnyDeniedMultiplePermissionsListener extends BaseMultiplePermissionsListener {
    private final Context context;
    private final String text;
    private final String buttonText;
    private final Component.ClickedListener onButtonClickListener;
    private final int duration;

    /**
     * @param context The view to find a parent from
     * @param text Message displayed in the snackbar
     * @param buttonText Message displayed in the snackbar button
     * @param onButtonClickListener Action performed when the user clicks the snackbar button
     */
    private PopupOnAnyDeniedMultiplePermissionsListener(
            Context context,
            String text,
            String buttonText,
            Component.ClickedListener onButtonClickListener,
            int duration) {
        this.context = context;
        this.text = text;
        this.buttonText = buttonText;
        this.onButtonClickListener = onButtonClickListener;
        this.duration = duration;
    }

    @Override
    public void onPermissionsChecked(MultiplePermissionsReport report) {
        super.onPermissionsChecked(report);
        DexterLogUtil.info("dexterLogUtil", "Listener-1");
        if (!report.areAllPermissionsGranted()) {
            PopupDialogUtils.getInstance().show(context, text, duration, buttonText, onButtonClickListener);
        }
    }

    /**
     * Builder class to configure the displayed snackbar
     * Non set fields will not be shown
     */
    public static class Builder {
        private final Context context;
        private final String text;
        private String buttonText;
        private Component.ClickedListener onClickListener;
        private int duration = 5000;

        private Builder(Context context, String text) {
            this.context = context;
            this.text = text;
        }
        /**
         * 初始化
         *
         * @param context context
         * @param text    text
         * @return Builder
         */
        public static Builder with(Context context, String text) {
            return new Builder(context, text);
        }
        /**
         * 初始化
         *
         * @param context        context
         * @param textResourceId textResourceId
         * @return Builder
         */
        public static Builder with(Context context, int textResourceId) {
            return Builder.with(context, ResTUtil.getString(context, textResourceId));
        }

        /**
         * Adds a text button with the provided click listener
         *
         * @param buttonText      按钮text
         * @param onClickListener listener
         * @return Builder
         */
        public Builder withButton(String buttonText, Component.ClickedListener onClickListener) {
            this.buttonText = buttonText;
            this.onClickListener = onClickListener;
            return this;
        }

        /**
         * Adds a text button with the provided click listener
         *
         * @param buttonTextResourceId id
         * @param onClickListener      c
         * @return builder
         */
        public Builder withButton(int buttonTextResourceId, Component.ClickedListener onClickListener) {
            return withButton(ResTUtil.getString(context, buttonTextResourceId), onClickListener);
        }

        /**
         * Adds a button that opens the application settings when clicked
         *
         * @param buttonText text
         * @return Builder
         */
        public Builder withOpenSettingsButton(String buttonText) {
            this.buttonText = buttonText;
            this.onClickListener = new SettingsClickListener(context);
            return this;
        }

        /**
         * Adds a button that opens the application settings when clicked
         *
         * @param buttonTextResourceId id
         * @return Builder
         */
        public Builder withOpenSettingsButton(int buttonTextResourceId) {
            return withOpenSettingsButton(ResTUtil.getString(context, buttonTextResourceId));
        }
        /**
         * Adds the duration of the snackbar on the screen
         *
         * @param duration 自动关闭时间
         * @return builder
         */
        public Builder withDuration(int duration) {
            this.duration = duration;
            return this;
        }

        /**
         * Builds a new instance of {@link PopupOnAnyDeniedMultiplePermissionsListener}
         *  @return Builder
         */
        public PopupOnAnyDeniedMultiplePermissionsListener build() {
            return new PopupOnAnyDeniedMultiplePermissionsListener(
                    context, text, buttonText, onClickListener, duration);
        }
    }
}
